package cc.wsyw126.java.lintCode;

import java.util.ArrayList;
import java.util.Stack;
/*Integer.parseInt(expression[i])  change string  to  int
 * Character.isDigit(); return boolean;
 * c can  use atoi()
 * write by yangDP
 * 2015.10.3
 * ��������ˣ�S.peek()=="("��S.peek().charAt(0)=='(',��������Eclipse�ж�ͨ����������lintcode����
 */

public class convertToRPN {

    public static void main(String[] args) {
        String[] expression = {"2", "*", "6", "-", "(", "23", "+", "7", ")", "/", "(", "1", "+", "2", ")"};
        int length = expression.length;
        ArrayList<String> list = new ArrayList<String>();
        Stack<String> S = new Stack<String>();
        for (int i = 0; i < length; i++) {
            if (Character.isDigit(expression[i].charAt(0))) {
                list.add(expression[i]);
            } else {
                switch (expression[i].charAt(0)) {
                    case '(':
                        S.push(expression[i]);
                        break;
                    case '+':
                    case '-':
                        while (!S.isEmpty())
                            if (S.peek() != "(") list.add(S.pop());
                            else break;
                        S.push(expression[i]);
                        break;
                    case '*':
                    case '/':
                        while (!S.isEmpty())
                            if (S.peek() == "*" || S.peek() == "/") list.add(S.pop());
                            else break;
                        S.push(expression[i]);
                        break;
                    case ')':
                        while (!S.isEmpty())
                            if (S.peek() != "(") list.add(S.pop());
                            else {
                                S.pop();
                                break;
                            }
                        break;
                }
            }
        }
        while (!S.isEmpty())
            list.add(S.pop());
        System.out.print(list);
    }

}
